package ranab.jar;

import java.util.zip.ZipEntry;


/**
 * Compare two zip entries based on the file name.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class ZipNameComparator extends ZipComparator  {
    
    public int compare(Object o1, Object o2)  {
        if (!(o1 instanceof java.util.zip.ZipEntry && o1 instanceof java.util.zip.ZipEntry))  {
            throw new IllegalArgumentException("Not a ZipEntry object."); 
        }
        
        ZipEntry z1 = (ZipEntry)o1;
        ZipEntry z2 = (ZipEntry)o2;
        return getName(z1).compareTo(getName(z2));
    }
    
    public boolean equals(Object o1, Object o2)  {
        if (!(o1 instanceof java.util.zip.ZipEntry && o1 instanceof java.util.zip.ZipEntry))  {
            throw new IllegalArgumentException("Not a ZipEntry object."); 
        }
        
        ZipEntry z1 = (ZipEntry)o1;
        ZipEntry z2 = (ZipEntry)o2;
        return getName(z1).equals(getName(z2));
    }
    
    /**
     * get the ZipEntry file name
     */
    public static String getName(ZipEntry entry)  {
        String fullName = entry.getName();
        int index = fullName.lastIndexOf('/');
        if (index == -1)      {
            return fullName;
        }
        else if (index == (fullName.length() -1))  {
                return "";
        }
        return fullName.substring(index + 1);
    }
    
    /**
     * get header name
     */
     public String getHeaderName()  {
         return "Name";
     }
}
